package com.tanhua.api.mongdb;

import com.tanhua.domain.mogo.RecommendUser;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class RecommentApiImpl implements RecommentApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    public RecommendUser findMaxScore(Long uid) {

        //条件查
        Query query = new Query(Criteria.where("toUserId").is(uid));
        query.with(new Sort(Sort.Direction.DESC,"score"));
        query.skip(0).limit(1);
        RecommendUser recommendUser=mongoTemplate.findOne(query,RecommendUser.class);
        return recommendUser;
    }

    @Override
    public PageBeanVo queryTodayBestList(Long uid, Integer page, Integer pagesize) {
        //条件查
        Query query = new Query(Criteria.where("toUserId").is(uid));
        query.with(new Sort(Sort.Direction.DESC,"score"));
        query.skip( (page-1)*pagesize).limit(pagesize);
        //数据
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        //总条数
        long count = mongoTemplate.count(query, RecommendUser.class);

        PageBeanVo pb = new PageBeanVo(page,pagesize,count,list);

        return pb;
    }

    @Override
    public Long findScore(Long id, Long jrid) {

        Query query = new Query(Criteria.where("toUserId").is(id).and("userId").is(jrid));

        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        //没查到 给假数据
        if(recommendUser==null){
            recommendUser=new RecommendUser();
            recommendUser.setScore(99d);
        }
        return recommendUser.getScore().longValue();

    }
}
